package com.calligraphy.user.repository;

import com.calligraphy.user.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import javax.transaction.Transactional;

/**
 * @Author hy
 * @Date 2018/12/27 10:35
 **/
public interface UserRepository extends JpaRepository<User,String> {

    @Transactional
    @Modifying
    @Query(value = "update user as u set u.enable =?1 where u.email = ?2",nativeQuery = true)
    int updateUserByEmail(int enable, String email);

    @Query(value = "select * from user as u where " +
            "if(:userName is not null,u.user_name=:userName,1=1) and if(:email is not null,u.email=:email,1=1) and if(:passWord is not null,u.pass_word=:passWord,1=1)",
            nativeQuery = true)
    User findBy3Param(@Param("userName")String userName, @Param("email")String email, @Param("passWord")String passWord);

    void deleteByEmail(String email);

    @Transactional
    @Modifying
    @Query(value = "update user as u set u.pass_word =?1 where u.email = ?2",nativeQuery = true)
    int updatePwdByEmail(String pwd, String email);

    User findByEmail(String email);

    User findByEmailAndPassWord(String email,String password);

    User findByUserIdAndPassWord(String userId,String password);

}
